<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline" style="display:none;">
            <label class="layui-form-label">账号：</label>
            <div class="layui-input-inline">
                <input type="text" name="username" lay-verify="title" style="width:100px;" placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">手机号码：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="mobile" lay-verify="title" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">性别：</label>
            <div class="layui-input-inline seller-inline-2" >
                <select name="sex" lay-verify="">
                    <option value=""></option>
                    <option value="1">男</option>
                    <option value="2">女</option>
                    <option value="3">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">昵称：</label>
            <div class="layui-input-inline seller-inline-2">
                <input type="text" name="nickname" lay-verify="title" placeholder="用户昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态：</label>
            <div class="layui-input-inline seller-inline-3">
                <select name="status" lay-verify="">
                    <option value=""></option>
                    <option value="1">正常</option>
                    <option value="2">停用</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">推荐人：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="pmobile" lay-verify="title" placeholder="推荐人手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>筛选</button>
                <button class="layui-btn layui-btn-sm" lay-submit  lay-filter="user-add"><i class="layui-icon">&#xe608;</i>添加</button>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm"  lay-submit lay-filter="export-user"><i class="iconfont icon-msnui-cloud-download" style="font-size: 20px !important;"></i>导出</button>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-user"><i class="layui-icon"></i>导入</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="userTable" lay-filter="test"></table>
</div>


<div id="exportUser" style="display: none;">
    <form class="layui-form export-form"  action="" >
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的会员，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>


<div id="importUser"  style="display: none;">
    <form class="layui-form import-form" method="post" action="{:url('ietask/import')}" enctype="multipart/form-data">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先下载<a href="{:url('ietask/importTemplete',['tplName'=>'user'])}">导入模板</a>
                </blockquote>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">
                    <input type="file" class="layui-file" name="importFile" id="importFile">
                    <input type="hidden" name="model" value="User">
                </label>
            </div>
        </div>
    </form>
</div>

<script src="__STATIC_JS__jquery.form.js" type="text/javascript" charset="utf-8"></script>
<script>
    var table,window_box;
    layui.use(['form', 'layedit', 'laydate','table'], function(){
        table = layui.table.render({
            elem: '#userTable',
            height: 'full-220',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            url: "{:url('user/index')}?_ajax=1",
            id:'userTable',
            cols: [[
                {type:'numbers'},
//                {field:'username', width:100,title:'账号'},
                {field:'mobile',  title:'手机号码',width:150},
                {field:'sex',  title:'性别'},
                {field:'birthday',title:'生日',width:150},
                {field:'avatar',title:'头像',
                    templet: function(d){
                        if (d.avatar) {
                            return '<a href="javascript:void(0);" onclick=viewImage("'+d.avatar+'")><image style="max-width:30px;max-height:30px;" src="'+d.avatar+'"/></a>';
                        }else{
                            return '<a href="javascript:void(0);" onclick=viewImage("<?php echo config('jshop.default_image'); ?>")><image style="max-width:30px;max-height:30px;" src="<?php echo config('jshop.default_image');?>"/></a>';
                        }
                    }, width: 80
                },
                {field:'nickname',title:'昵称'},
                { title:'余额',templet:function(data){
                        var html = '<a class="link-hot option-show balance" data-id="' + data.id + '">'+data.balance+'</a>';
                        return html;
                    }},
                { title:'积分',templet:function(data){
                        var html = '<a class="link-hot option-show point" data-id="' + data.id + '">'+data.point+'</a>';
                        return html;
                    }},

                {field:'status',title:'状态'},
                {field:'pid_name', title: '邀请人'},
                {field:'ctime', title:'创建时间',width:200},
                {field:'option',title:'操作', templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-xs edit" data-id="' + d.id + '">修改</a>';
                        html += '<a class="layui-btn layui-btn-xs editMoney" data-id="' + d.id + '">修改余额</a>';
                        html += '<span class="layui-btn layui-btn-xs pointEdit" data-id="'+d.id+'">积分修改</span>';
                        return html;


                    }, align:'center', fixed:'right',width:200}
            ]]
        });
        //搜索
        layui.form.on('submit(*)', function(data){
            layui.table.reload('userTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //积分编辑
        $(document).on('click', '.pointEdit', function () {
            var id = $(this).attr('data-id');
            $.ajax({
                url: "{:url('manage/user/editPoint')}",
                type: 'get',
                data: {
                    'user_id': id,
                    'flag': 'false'
                },
                success: function (e) {
                    layer.open({
                        type: 1,
                        title: '修改用户积分',
                        area: ['450px', '300px'], //宽高
                        content: e
                    });
                }
            });
        });
        //保存积分
        $(document).on('click', '.edit-point-save', function(){
            var user_id = $("#user_id").val();
            var point = $("#point").val();
            var memo = $("#memo").val();
            if($.trim(point) == ''){
                layer.msg('积分变更不能为空');
            }
            $.ajax({
                url: "{:url('manage/user/editPoint')}",
                type: 'post',
                data: {
                    'user_id': user_id,
                    'flag': 'true',
                    'point': point,
                    'memo': memo
                },
                success: function (e) {
                    if(e.status){
                        layer.msg(e.msg, {time: 1500}, function(){
                            layer.closeAll();
                            layui.table.reload('userTable');
                        });
                    }else{
                        layer.msg(e.msg);
                    }
                }
            });
        });
        //积分记录
        $(document).on('click', '.point', function () {
            var id = $(this).attr('data-id');
            $.ajax({
                url: "{:url('manage/user/pointLog')}",
                type: 'get',
                data: {
                    'user_id': id,
                    'flag': 'false'
                },
                success: function (e) {
                    layer.open({
                        type: 1,
                        title: '用户积分记录',
                        area: ['800px', '535px'], //宽高
                        content: e
                    });
                }
            });
        });

        //添加
        layui.form.on('submit(user-add)',function(){
            $.ajax({
                type:'get',
                url: "{:url('User/addUser')}",
                success:function(e){
                    window.box = layer.open({
                        type: 1,
                        content: e,
                        area: ['700px', '550px'],
                        title:'添加用户',
                        btn: ['确定','取消'],
                        yes: function(){
                            var data = $("#userAdd").serializeArray();
                            $.ajax({
                                type: 'post',
                                url: '{:url("User/addUser")}',
                                data: data,
                                dataType: 'json',
                                success: function (e) {
                                    if(e.status){
                                        layer.close(window.box);
                                        layer.msg(e.msg, {time:1300},function(){
                                            layui.table.reload('userTable');
                                        });
                                    }else{
                                        layer.msg(e.msg);
                                    }
                                }
                            });
                        }
                    });
                }
            });
            return false;
        });
        //编辑
        $(document).on('click', '.edit', function(){
            var user_id = $(this).attr('data-id');
            $.ajax({
                url: '{:url("User/editUser")}',
                data: {
                    user_id: user_id
                },
                type: 'get',
                success: function (e) {
                    window.box = layer.open({
                        type: 1,
                        content: e,
                        area: ['700px', '550px'],
                        title:'编辑用户',
                        btn: ['确定','取消'],
                        yes: function(){
                            var data = $("#userEdit").serializeArray();
                            $.ajax({
                                type: 'post',
                                url: '{:url("User/editUser")}',
                                data: data,
                                dataType: 'json',
                                success: function (e) {
                                    if(e.status){
                                        layer.close(window.box);
                                        layer.msg(e.msg, {time:1300},function(){
                                            layui.table.reload('userTable');
                                        });
                                    }else{
                                        layer.msg(e.msg);
                                    }
                                }
                            });
                        }
                    });
                }
            });
        });

        //余额编辑
        $(document).on('click', '.editMoney', function () {
            var id = $(this).attr('data-id');
            $.ajax({
                url: "{:url('manage/user/editMoney')}",
                type: 'get',
                data: {
                    'user_id': id,
                    'flag': 'false'
                },
                success: function (e) {
                    layer.open({
                        type: 1,
                        title: '修改用户余额',
                        area: ['450px', '300px'], //宽高
                        content: e
                    });
                }
            });
        });
        //保存余额
        $(document).on('click', '.edit-money-save', function(){
            var user_id = $("#user_id").val();
            var money = $("#money").val();
            $.ajax({
                url: "{:url('manage/user/editMoney')}",
                type: 'post',
                data: {
                    'user_id': user_id,
                    'flag': 'true',
                    'money': money
                },
                success: function (e) {
                    if(e.status){
                        layer.msg(e.msg, {time: 1500}, function(){
                            layer.closeAll();
                            layui.table.reload('userTable');
                        });
                    }else{
                        layer.msg(e.msg);
                    }
                }
            });
        });
        //余额明细
        $(document).on('click', '.balance', function () {

            var id = $(this).attr('data-id');
            window.location.href="{:url('manage/balance/index')}"+'?user_id='+id;
        });


        //会员导出
        layui.form.on('submit(export-user)', function(data){

            layer.open({
                type: 1,
                title:'会员导出',
                area: ['400px', '290px'], //宽高
                btn:['确定','取消'],
                content: $("#exportUser").html(),
                yes:function () {
                    //判断是否有选中
                    var filter = $(".seller-form").serialize();
                    $(".export-form:last").append("<input type='hidden' name='filter' value='"+filter+"'>");
                    var data = $(".export-form:last").serializeArray();
                    data.push({'name':'model','value':'User'});
                    JsPost("{:url('Ietask/export')}",data,function(res){
                            layer.msg(res.msg, {time:1500}, function(){
                                if(res.status){
                                    table.reload();
                                    layer.closeAll();
                                }
                            });
                        }
                    );
                },btn2:function () {
                    layer.closeAll();
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //会员导入
        layui.form.on('submit(import-user)', function(data) {
            layer.open({
                type: 1,
                title:'会员导入',
                area: ['400px', '290px'], //宽高
                btn:['确定','取消'],
                content: $("#importUser").html(),
                yes:function (index,obj) {
                    $(obj).find('.import-form').ajaxSubmit({
                        type:'post',
                        dataType:'json',
                        success:function(result){
                            layer.msg(result.msg, {time:1500}, function(){
                                if(result.status){
                                    table.reload();
                                    layer.closeAll();
                                }
                            });
                        },
                        error:function(result){
                            layer.msg("导入失败");
                        }
                    });
                },btn2:function () {
                    layer.closeAll();
                }
            });
        });
    });
</script>
